<template>
  <div class="auth-container">
    <div id="water-wave"></div>
    <form class="auth-form" @submit.prevent="handleSubmit">
      <h2>用户注册</h2>

      <div class="form-group">
        <label>用户名</label>
        <input
          type="text"
          v-model="formData.username"
          placeholder="4-16位字母数字组合"
          @blur="validateUsername"
        />
        <span class="error-msg">{{ errors.username }}</span>
      </div>

      <div class="form-group">
        <label>电子邮箱</label>
        <input
          type="email"
          v-model="formData.email"
          placeholder="请输入有效邮箱"
          @blur="validateEmail"
        />
        <span class="error-msg">{{ errors.email }}</span>
      </div>

      <div class="form-group">
        <label>密码</label>
        <input
          type="password"
          v-model="formData.password"
          placeholder="至少包含字母和数字"
          @input="validatePassword"
        />
        <span class="error-msg">{{ errors.password }}</span>
      </div>

      <div class="form-group">
        <label>确认密码</label>
        <input
          type="password"
          v-model="formData.confirmPassword"
          placeholder="请再次输入密码"
          @input="validateConfirmPassword"
        />
        <span class="error-msg">{{ errors.confirmPassword }}</span>
      </div>

      <div class="terms">
        <input type="checkbox" v-model="formData.agreeTerms" id="terms" />
        <label for="terms">我已阅读并同意<router-link to="/terms">服务条款</router-link></label>
        <span class="error-msg">{{ errors.agreeTerms }}</span>
      </div>

      <button type="submit" class="auth-btn" :disabled="isSubmitting">
        {{ isSubmitting ? '注册中...' : '立即注册' }}
      </button>

      <div class="auth-links">
        <span>已有账号？</span>
        <router-link to="/login">立即登录</router-link>
      </div>
    </form>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const formData = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: false,
})

const errors = reactive({
  username: '',
  email: '',
  password: '',
  confirmPassword: '',
  agreeTerms: '',
})

const isSubmitting = ref(false)

const validateUsername = () => {
  const usernameRegex = /^[a-zA-Z0-9]{4,16}$/
  if (!formData.username) {
    errors.username = '用户名不能为空'
  } else if (!usernameRegex.test(formData.username)) {
    errors.username = '4-16位字母数字组合'
  } else {
    errors.username = ''
  }
}

const validateEmail = () => {
  const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/
  if (!formData.email) {
    errors.email = '邮箱不能为空'
  } else if (!emailRegex.test(formData.email)) {
    errors.email = '邮箱格式不正确'
  } else {
    errors.email = ''
  }
}

const validatePassword = () => {
  const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d).{8,}$/
  if (!formData.password) {
    errors.password = '密码不能为空'
  } else if (!passwordRegex.test(formData.password)) {
    errors.password = '至少8位且包含字母和数字'
  } else {
    errors.password = ''
    validateConfirmPassword()
  }
}

const validateConfirmPassword = () => {
  if (!formData.confirmPassword) {
    errors.confirmPassword = '请确认密码'
  } else if (formData.password !== formData.confirmPassword) {
    errors.confirmPassword = '两次输入密码不一致'
  } else {
    errors.confirmPassword = ''
  }
}

const validateForm = () => {
  validateUsername()
  validateEmail()
  validatePassword()
  validateConfirmPassword()

  if (!formData.agreeTerms) {
    errors.agreeTerms = '必须同意服务条款'
  } else {
    errors.agreeTerms = ''
  }

  return !Object.values(errors).some((error) => error)
}

const handleSubmit = async () => {
  if (!validateForm()) return

  try {
    isSubmitting.value = true
    // 模拟API请求
    await new Promise((resolve) => setTimeout(resolve, 1500))

    // 注册成功后跳转到登录页面
    router.push('/login')
    alert('注册成功，请登录！')
  } catch (error) {
    alert(error.message || '注册失败，请稍后重试')
  } finally {
    isSubmitting.value = false
  }
}
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  position: relative;
  overflow: hidden; /* 防止波纹溢出 */
  background: linear-gradient(135deg, #4facfe, #00f2fe); /* 水利主题渐变背景 */
}

#water-wave {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 20%, transparent 80%);
  animation: wave 8s infinite ease-in-out;
  transform: translate(-50%, -50%);
  z-index: -1; /* 确保波纹背景在后面 */
}

#water-wave::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.15) 20%, transparent 80%);
  animation: wave 10s infinite ease-in-out reverse;
  transform: translate(-50%, -50%);
}

#water-wave::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 300%;
  height: 300%;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.1) 20%, transparent 80%);
  animation: wave 12s infinite ease-in-out;
  transform: translate(-50%, -50%);
}

.auth-form {
  background: rgba(255, 255, 255, 0.8); /* 半透明白色背景 */
  padding: 2.5rem 3rem;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1); /* 柔和阴影 */
  width: 420px;
  animation: fadeIn 0.5s ease-in-out;
}

h2 {
  text-align: center;
  color: #333;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: bold;
  position: relative;
}

h2::after {
  content: '';
  display: block;
  width: 50px;
  height: 4px;
  background: #4facfe; /* 水利主题颜色 */
  margin: 0.5rem auto 0;
  border-radius: 2px;
}

.form-group {
  margin-bottom: 1.5rem;
}

label {
  display: block;
  margin-bottom: 0.5rem;
  color: #555;
  font-weight: 500;
}

input[type='text'],
input[type='password'],
input[type='email'] {
  width: 100%;
  padding: 0.9rem;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 1rem;
  transition:
    border-color 0.3s,
    box-shadow 0.3s;
}

input:focus {
  outline: none;
  border-color: #4facfe; /* 聚焦时的水利主题颜色 */
  box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.2);
}

.error-msg {
  color: #f56c6c;
  font-size: 0.875rem;
  margin-top: 0.25rem;
  display: block;
}

.terms {
  margin: 1.5rem 0;
  display: flex;
  align-items: center;
}

.terms input {
  margin-right: 0.5rem;
}

.terms a {
  color: #4facfe;
  margin-left: 0.3rem;
  text-decoration: underline;
}

.auth-btn {
  width: 100%;
  padding: 0.9rem;
  background: linear-gradient(135deg, #4facfe, #00f2fe); /* 水利主题按钮渐变 */
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1rem;
  font-weight: bold;
  transition:
    background 0.3s,
    transform 0.2s;
}

.auth-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #00f2fe, #4facfe); /* 悬停时渐变反转 */
  transform: translateY(-2px);
}

.auth-btn:disabled {
  background: #a0cfff;
  cursor: not-allowed;
}

.auth-links {
  margin-top: 2rem;
  text-align: center;
  color: #666;
}

.auth-links a {
  color: #4facfe;
  margin-left: 0.5rem;
  text-decoration: none;
  font-weight: 500;
}

.auth-links a:hover {
  text-decoration: underline;
}

@keyframes wave {
  0% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
  50% {
    transform: translate(-50%, -50%) scale(1.1);
    opacity: 0.7;
  }
  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
  }
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>
